#!/usr/bin/python 
#vim: set fileencoding:utf-8
import requests
from bs4 import BeautifulSoup
import MySQLdb
import time
def get_star():
  headers = {'user_agent':'Mozilla/5.0 (Windows NT 6.1;Win64;x64) AppletWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36','Host':'movie.douban.com'}
  movie_list = []
  for i in range(0,10):
   link = 'https://movie.douban.com/top250?start='+str(i*25)
   r = requests.get(link,headers=headers,timeout=10)
   print(str(i+1),"status_code",r.status_code)
   soup = BeautifulSoup(r.text,'html.parser')
   div_list = soup.find_all('div',class_='star')
   for each in div_list:
    movie = each.find_all('span')
    movie_list.append(movie[1].text.strip())
  return movie_list

def get_name():
 headers = {'user_agent':'Mozilla/5.0 (Windows NT 6.1;Win64;x64) AppletWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36','Host':'movie.douban.com'}
 movie_name = []
 for i in range(0,10):
   link = 'https://movie.douban.com/top250?start='+str(i*25)
   r = requests.get(link,headers=headers,timeout=10)
   #print(str(i+1),"status_code",r.status_code)
   soup = BeautifulSoup(r.text,'html.parser')
   div_name = soup.find_all('div',class_='hd')
   for each in div_name:
    name = each.a.span.text.strip()
    movie_name.append(name)
 return movie_name

start = time.time()
names = get_name()
movies = get_star()
#conn = MySQLdb.connect(host='localhost',user='root',passwd='',db='test1',charset='utf8')
#cur = conn.cursor()
for i in range(0,len(names)):
 print(str(i+1)+' '+'name:'+names[i]+' '+'star:'+movies[i])
end = time.time()
print("Exiting programming",end-start)
 #cur.execute("insert into douban values (%s,%s);",(names[i],movies[i]))
#cur.close()
#conn.commit()
#conn.close()

 
